package com.aidex.web.controller.biz;

import com.aidex.biz.domain.FriendsLinks;
import com.aidex.biz.service.FriendsLinksService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 友情小程序链接Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/biz/bizFriendsLinks")
public class FriendsLinksController extends BaseController {
    @Autowired
    private FriendsLinksService friendsLinksService;

    /**
     * 查询友情小程序链接列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:list')")
    @GetMapping("/list")
    public R<PageInfo> list(FriendsLinks friendsLinks, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            friendsLinks.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        friendsLinks.setPage(new PageDomain(request, response));
        return R.data(friendsLinksService.findPage(friendsLinks));
    }

    /**
     * 获取友情小程序链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:query')")
    @GetMapping(value = "/{id}")
    public R<FriendsLinks> detail(@PathVariable("id") String id) {
        return R.data(friendsLinksService.get(id));
    }

    /**
     * 新增友情小程序链接
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:add')")
    @Log(title = "友情小程序链接", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated FriendsLinks friendsLinks) {
        return R.status(friendsLinksService.save(friendsLinks));
    }

    /**
     * 修改友情小程序链接
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:edit')")
    @Log(title = "友情小程序链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated FriendsLinks friendsLinks) {
        return R.status(friendsLinksService.save(friendsLinks));
    }


    /**
     * 删除友情小程序链接
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:remove')")
    @Log(title = "友情小程序链接", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(friendsLinksService.deleteFriendsLinksByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(friendsLinksService.findMaxSort(new FriendsLinks()));
    }

    /**
     * 导出友情小程序链接列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizFriendsLinks:export')")
    @Log(title = "友情小程序链接", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(FriendsLinks friendsLinks) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            friendsLinks.setCompanyId(null);
        } else {
            friendsLinks.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<FriendsLinks> list = friendsLinksService.findList(friendsLinks);
        ExcelUtil<FriendsLinks> util = new ExcelUtil<FriendsLinks>(FriendsLinks.class);
        return util.exportExcel(list, "友情小程序链接数据");
    }

}
